package helper

import (
	"fmt"
	"log"
	"net/http"
	"strconv"
)

func InitWeb(port string)  {
	http.HandleFunc("/music", func(w http.ResponseWriter, r *http.Request) {
		id, _ := strconv.ParseInt(r.FormValue("id"),10,64)
		zl := Music(int(id))
		fmt.Println("音乐直链",zl.Data[0].Url)
		//do, err := req.C().R().
		//	SetHeader("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36").
		//	SetHeader("Accept","*/*").
		//	SetHeader("Accept-Encoding","identity;q=1, *;q=0").
		//  SetHeader("Accept-Language","zh-CN,zh;q=0.9").
		//	SetHeader("Referer",zl.Data[0].Url).
		//	Get(zl.Data[0].Url)
		//if err != nil {
		//	return
		//}
		//fmt.Println(do.StatusCode)
		//switch do.StatusCode {
		//case 200:
		//	all, err := io.ReadAll(do.Body)
		//	if err != nil {
		//		return
		//	}
		//	file, err := os.OpenFile("demo.mp3", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
		//	if err != nil {
		//		return
		//	}
		//	_, err = file.Write(all)
		//	if err != nil {
		//		return
		//	}
		//}
		//fmt.Println(do.Response)
		//fmt.Println("location-->",do.GetHeader("Location"))

		fmt.Println(zl.Code)
		if zl.Code != 200 {
			errorpng := "https://gitee.com/static/errors/images/404.png"
			http.Redirect(w, r, errorpng, 302)
			return
		}
		http.Redirect(w, r, zl.Data[0].Url, 302)
	})
	http.HandleFunc("/lrc", func(w http.ResponseWriter, r *http.Request) {
		id, _ := strconv.ParseInt(r.FormValue("id"),10,64)
		zl := Lyric(int(id))
		_, err := w.Write([]byte(zl))
		if err != nil {
			return
		}
	})
	fmt.Printf("服务启动在\n" +
		"http://localhost:%s/music?id=\n" +
		"http://localhost:%s/lrc?id=\n", port,port)
	log.Fatal(http.ListenAndServe(":"+port, nil))

}
